#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2018/10/30 16:54
@desc
"""

from __future__ import unicode_literals
from threadpool.pool_task.BaseTask import BaseTask


class TaskScheduler:

    def __init__(self, **kwargs):
        self.log = kwargs['log']

    def timer_scheduler(self, tasks, *args, **kwargs):
        """
        :desc 从数据库中抓取出需要调度的原始任务
        :param tasks:
        :param moniDate:
        :return: list[tasks]
        """
        schds = tasks
        return schds

    def call_back(self, trd):
        """
        执行的回调逻辑
        :param trd:
        :return:
        """
        stat, task_id, task_name = trd.result()
        # print('任务 %s - %s 执行结果是:%s' % (task_id, task_name, stat))
        self.log.info('任务 %s - %s 执行结果是:%s', task_id, task_name, stat)

    def scheduler_main(self, tasks, **kwargs):
        """
        任务调度器
        :param tasks:
        :param moniDate:
        :return:
        """
        moniDate = kwargs['moniDate'] if kwargs.has_key('moniDate') else "2019-02-18 00:59:59"
        print(moniDate)
        schdl_tasks = self.timer_scheduler(tasks, moniDate)
        self.log.info('开始调度')
        # taskExecutor = TaskExecutor(log)
        # task_stat_results = taskExecutor.execute_task(schdl_tasks)
        # print(len(task_stat_results))
        # for trd in task_stat_results:
        #     self.call_back(trd)

if __name__ == '__main__':
    import os
    from threadpool.pool_task.plogging import Plog

    logFormatDict = {
        'standard_format': '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \
                           '[%(levelname)s][%(message)s]',  # 其中name为getlogger指定的名字
        'simple_format': '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s',
        'id_simple_format': '[%(levelname)s][%(asctime)s] %(message)s',
        'simple_format_info': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    }
    loggerLevel = 'DEBUG:INFO'
    loggerFormat = 'standard_format'
    loggerDir = r'D:\iProject\myPython\com\teradata\threadpool\pool_task\logs'
    loggerFileName = 'sync_system.log'
    logFileName = loggerDir + os.sep + loggerFileName

    log = Plog()
    log.set_logger(when='D', limit=1, level=loggerLevel, filename=logFileName, mode='w',
                   fmt=logFormatDict[loggerFormat])
    log = log.get_logger_instance()

    log.info('test')

    tasks = [('1', 'task1'), ('2', 'task2'), ('3', 'task3')]
    tasks = (BaseTask(ot[0], ot[1]) for ot in tasks)

    # taskScheduler = TaskScheduler(log=log)
    # taskScheduler.scheduler_main(tasks, moniDate="2019-02-18 00:59:59")


